package com.example.springbootdatamock.mock;

import com.apifan.common.random.RandomSource;
import com.apifan.common.random.source.PersonInfoSource;
import com.example.springbootdatamock.bean.Customer;
import com.example.springbootdatamock.mapper.ESMapper;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Component
@RestController
public class ESMocker
        implements Runnable {
    @Autowired
    private ESMapper esMapper;
    @Value("${sleepMs.esMockSleep}")
    private Integer mockSleep;

    @RequestMapping("/esMock")
    public void run() {
         Customer customer = new Customer();
         PersonInfoSource personInfoSource = RandomSource.personInfoSource();
         int id = 0;
         int sexRand = 0;
         String[] source = {"搜索引擎", "直接访问", "邮件营销", "联盟广告", "视频广告"};
         String[] profession = {"电子网络", "销售", "教育", "保险", "银行", "建筑", "传媒"};


        while (true) {
             sexRand = RandomUtils.nextInt(0, 2);
             customer.setId(Integer.valueOf(id++));
             if (sexRand == 0) {
                 customer.setSex("男");
                 customer.setName(personInfoSource.randomMaleChineseName());
            } else {
                 customer.setSex("女");
                 customer.setName(personInfoSource.randomFemaleChineseName());
            }
             customer.setSource(source[RandomUtils.nextInt(0, source.length)]);
             customer.setProfession(profession[RandomUtils.nextInt(0, profession.length)]);

             this.esMapper.save(customer);

            try {
                 Thread.sleep(this.mockSleep.intValue());

            } catch (InterruptedException e) {
                 e.printStackTrace();
            }
        }
    }
}



